package org.javaopenaccess.web.ui.classes;


import org.javaopenaccess.forms.ClassForm;
import org.javaopenaccess.service.classes.DynamicClassService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zkoss.zkplus.spring.SpringUtil;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

public class NewClassController extends Window {

	private static final long serialVersionUID = 2946481284135969352L;
	private Logger logger = LoggerFactory.getLogger(this.getClass().getName());
	private DynamicClassService dynamicClassService;
	
	public void onAdd(){
		logger.info("WTF");
		String name = ((Textbox)this.getFellow("name")).getValue().trim();
		String type = ((Textbox)this.getFellow("type")).getValue().trim();
		String defValue = ((Textbox)this.getFellow("default")).getValue().trim();
		ClassForm form = new ClassForm(name);
		Class<?> clazz = dynamicClassService.createType(form);
		try {
			dynamicClassService.createTypeInstance(clazz);
		} catch (InstantiationException e) {
			e.printStackTrace();
		}
	}
	//catch events with...
	//public void onOK(org.zkoss.zk.ui.event.KeyEvent event) {}
	//public void onOK(org.zkoss.zk.ui.event.Event event) {}

	public void onCreate(){
		logger.info("fetching spring beans");
		try {
			dynamicClassService = (DynamicClassService)
				SpringUtil.getBean("dynamicClassService");
		}catch (IllegalStateException ex){
			logger.error("error, could not load spring beans!", ex);
		}
	}

}
